#include <string.h>
#include <stdio.h>
#include "nano_db.h"

struct geranParam_t
{
    uint32_t   frequency;
    uint16_t   band;
    uint8_t    mcc;
    uint8_t    mnc;
    uint32_t   lac;
    uint8_t    ncell;
    uint32_t   rssiMin;
    uint32_t   thresholdDelta;
    uint16_t   duration;
};

struct ndb_ns_t root__ns = {"config", "http://skynetworks.com.cn/gsm/bts/config/2.0.0"};
struct ndb_ns_t communication__ns = {"communication", "http://skynetworks.com.cn/gsm/bts/communication/2.0.0"};
struct ndb_ns_t sniffer__ns = {"sniffer", "http://skynetworks.com.cn/gsm/bts/sniffer/2.0.0"};
struct ndb_ns_t geran__ns = {"geran", "http://skynetworks.com.cn/gsm/bts/geran/2.0.0"};

int get_geranParam(struct ndb_t* ndb)
{
    int n;
    struct ndb_ctx_t ctx;
    struct ndb_tabset_t set;

    ndb_create_context(ndb, &geran__ns, &ctx);

    struct ndb_ns_t* nslist[] = {&root__ns, &geran__ns};
    ndb_register_namespaces(&ctx, nslist, 2);

    const char* xpath = "/config:config/geran:geran/geran:carrier";
    n = ndb_query(ndb, &ctx, xpath, &set);
    if (n == -1) {
        printf("no element found in path %s\n", xpath);
        return -1;
    }

    int j;
    ndb_elem_ptr rootp;
    ndb_elset_ptr elset = get_elem_set(&set);
    char buf[64];
    struct geranParam_t *param;

    for_each_subtree(elset, rootp, j) 
    {
        param = (struct geranParam_t*)ndb_get_container(rootp, (void*)buf);

        printf("frequency = %u, band = %u, mcc = %u, mnc = %u, lac = %u, ncell = %u, rssiMin = %u, thresholdDelta = %u, duration = %u\n",
                param->frequency, param->band, param->mcc, param->mnc, param->lac, param->ncell, 
                param->rssiMin, param->thresholdDelta, param->duration);
    }

    ndb_destory_context(&ctx);

    return 0;     
}

int main()
{
    struct ndb_t ndb;
    ndb_create(&ndb, "gsmcfg.xml");


    get_geranParam(&ndb);

    ndb_destory(&ndb);    
    return 0;
}
